package org.yray.sip.rtp.sender;


import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.buf.HexUtils;
import org.yray.sip.utils.ByteUtils;

import java.util.Arrays;

/***
 *
 200    SR（Sender Report）   发送端报告
 201    RR（Receiver Report） 接收端报告
 202    SDES（Source Description Items）  源点描述
 203    BYE 结束传输
 204    APP 特定应用
 */
@Slf4j
public abstract class  RtcpPacket implements PacketInte{



    protected byte[] packet;
    protected long ssrc;


    public long getSsrc() {
        return ssrc;
    }

    public void setSsrc(long ssrc) {
        this.ssrc = ssrc;
    }

    public int getHEX_SR() {
        return HEX_SR;
    }

    public void setHEX_SR(int HEX_SR) {
        HEX_SR = HEX_SR;
    }


    /***
     * 81|c9|0007|452670d5      rtcp版本2固定81|c9代表类型201|长度|SSRC|
     00000000|00|000000|0000|0000|00000000|  Identification|fraction lost|cum lost|seq|high seq|jitter|
     00000000|00000000
     * @return
     */
    public static byte[] builtRR(long ssrc)
    {
        byte[] result= new byte[32];
        Arrays.fill(result,(byte)0);
        result[3]=7;
        System.arraycopy(ByteUtils.uIntToByteWord(HEX_RR),0,result,0,2);
        System.arraycopy(ByteUtils.uLongToByteWord(ssrc),0,result,4,4);
        return result;
    }

    public static byte[] builtSR(long ssrc,RCPacket rcPacket)
    {
        byte[] result= new byte[32];
        Arrays.fill(result,(byte)0);
        result[3]=12;
        System.arraycopy(ByteUtils.uIntToByteWord(HEX_SR),0,result,0,2);
        System.arraycopy(ByteUtils.uLongToByteWord(ssrc),0,result,4,4);
        System.arraycopy(ByteUtils.uLongToByteWord(rcPacket.getMsw()),0,result,8,4);
        System.arraycopy(ByteUtils.uLongToByteWord(rcPacket.getLsw()),0,result,12,4);
        System.arraycopy(ByteUtils.uLongToByteWord(rcPacket.getRtpTimeStamp()),0,result,16,4);
        System.arraycopy(ByteUtils.uLongToByteWord(rcPacket.getPacketCount()),0,result,20,4);
        System.arraycopy(ByteUtils.uLongToByteWord(rcPacket.getOctet()),0,result,24,4);
        return result;
    }

    /**RRPacket
     * 81|ca|0006|452670d5
     * 01|10|30666161336133303736666532643634|0000 cname|len|text的长度就是前面的len
     * @param ssrc
     * @return
     */
    public static byte[] builtSDES(long ssrc,String cname)
    {
        byte[] hexCname=cname.getBytes();
        byte[] result= new byte[12+hexCname.length];
        result[2]=0;
        result[3]=6;
        result[8]=1; //cname
        result[9]=(byte)hexCname.length;
        System.arraycopy(ByteUtils.uIntToByteWord(HEX_SDES),0,result,0,2);
        System.arraycopy(ByteUtils.uLongToByteWord(ssrc),0,result,4,4);

        System.arraycopy(hexCname,0,result,10,hexCname.length);
        result[result.length-2]=0;
        result[result.length-1]=0;
        return result;
    }


    public static RCPacket formatRtcp(byte[] bs)
    {
        String ssrc= HexUtils.toHexString(new byte[]{bs[4],bs[5],bs[6],bs[7]});
        long msw=ByteUtils.uBytes2Long(bs[8],bs[9],bs[10],bs[11]);
        long lsw=ByteUtils.uBytes2Long(bs[12],bs[13],bs[14],bs[15]);
        long rtpTimeStamp=ByteUtils.uBytes2Long(bs[20],bs[21],bs[22],bs[23]);
        long packetCount=ByteUtils.uBytes2Long(bs[24],bs[25],bs[26],bs[27]);
        long octet=ByteUtils.uBytes2Long(bs[28],bs[29],bs[30],bs[31]);
        log.debug("ssrc={},msw={},lsw={},rtpTimeStamp={},packetCount={},octet={}",ssrc,msw,lsw,rtpTimeStamp,packetCount,octet);
        RCPacket rcPacket=new RCPacket();
        rcPacket.setLsw(lsw);
        rcPacket.setMsw(msw);
        rcPacket.setOctet(octet);
        rcPacket.setPacketCount(packetCount);
        rcPacket.setRtpTimeStamp(rtpTimeStamp);
        rcPacket.setSsrcStr(ssrc);
        return rcPacket;

    }
}
